import type { Effect, Reducer } from 'umi';
import { message } from 'antd';
import { getToken } from '@/utils/storage';

export type loginState = {
  isLogin?: '';
};

export type NewsType = {
  namespace: 'login';
  state: loginState;
  effects: {
    fetchIsLogin: Effect;
  };
  reducers: {
    setLogin: Reducer<loginState>;
  };
};

// @ts-ignore
const loginModel = {
  namespace: 'login',
  state: {
    isLogin: !!getToken(),
  },
  effects: {
    *fetchIsLogin({ payload }, { call, put }) {
      try {
        yield put({
          type: 'setLogin',
          payload: payload,
        });
      } catch (err) {
        message.error('登录失败').then();
      }
    },
  },
  reducers: {
    setLogin(state, { payload }) {
      return {
        ...state,
        isLogin: payload,
      };
    },
  },
};
export default loginModel;
